package com.young.shop.controller;

import com.young.shop.pojo.entity.User;
import com.young.shop.pojo.vo.Message;
import com.young.shop.pojo.vo.ResponseEntity;
import com.young.shop.pojo.vo.Status;
import com.young.shop.service.ShoppingCartService;
import com.young.shop.service.impl.ShoppingCartServiceImpl;
import com.young.shop.utils.ResponseUtils;
import com.young.shop.utils.ShoppingCartData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LY
 * @date 2020/12/19 9:27
 */
@WebServlet("/ShoppingCartController.do")
public class ShoppingCartController extends HttpServlet {
    ShoppingCartService service = new ShoppingCartServiceImpl();
    private final String CHARACTER_ENCODING = "utf-8";
    //作为操作的一个标识
    public final String ANCTION = "anction";
    public final String GET_DATA = "getData";
    public final String UPDATE_DATA = "updateData";
    public final String DELETE_DATA = "deleteData";
    public final String USER_ID = "userId";
    public final String COMMODITY_ID_LIST = "commodityIdList";
    public final String COMMODITY_ID = "commodityId";
    public final String NUMBER = "number";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从数据库取值
        request.setCharacterEncoding(CHARACTER_ENCODING);
        //返回值
        ResponseEntity entity = new ResponseEntity();
        User oldUser = (User)request.getSession().getAttribute("client");
        int id = (int) oldUser.getId();
        //如果返回是数字字符串，直接返回，如果返回是对象，取对应的key值，再返回
        try {
            ShoppingCartData data = null;
          //  int userId = 0;
            String commodityId = null;
            int number = 0;
            String[] commodityIdList = null;
            String anction = request.getParameter(ANCTION);
            switch (anction){
                case GET_DATA:
                    id = Integer.parseInt(request.getParameter(USER_ID));
                    data = service.getShoppingCartDataByUserId(id);
                    entity.setData(data);
                    entity.setMsg(Message.SUCCESS);
                    entity.setStatus(Status.SUCCESS);
                    break;
                case UPDATE_DATA:
                    //获取id
                    id = Integer.parseInt(request.getParameter(USER_ID));
                    commodityId = request.getParameter(COMMODITY_ID);
                    number = Integer.parseInt(request.getParameter(NUMBER));
                    data = service.updateShoppingCartNumber(id,commodityId,number);
                    entity.setData(data);
                    entity.setMsg(Message.SUCCESS);
                    entity.setStatus(Status.SUCCESS);
                    break;
                case DELETE_DATA:
                 id = Integer.parseInt(request.getParameter(USER_ID));
                    commodityIdList = request.getParameterValues(COMMODITY_ID_LIST);
                    if(commodityIdList != null){
                        List<String> list = new ArrayList<String>(Arrays.asList(commodityIdList));
                        data = service.clearShoppingCartByUserId(id,list);
                        entity.setData(data);
                        entity.setMsg(Message.SUCCESS);
                        entity.setStatus(Status.SUCCESS);
                    }
                    break;
                default:
                    entity.setMsg(Message.ERROR);
                    entity.setStatus(Status.ERROR);
            }
        }catch (Exception e){
            entity.setMsg(Message.ERROR);
            entity.setStatus(Status.ERROR);
        }
        //给前台传数据
        ResponseUtils.responseToJson(response,entity);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
